package com.rysm.wjsm.Demo6;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MyRunnable implements Runnable {
     static int[] arr = {10,5,20,50,100,200,500,800,2,80,300,700};
     static ArrayList<Integer> list1 = new ArrayList<>();
       static int max;

     static {
        for (int i : arr) {
            list1.add(i);
            max = Collections.max(list1);
        }
    }
    @Override
    public void run() {
        Random r = new Random();
        Thread thread = Thread.currentThread();
        ArrayList<Integer> box = new ArrayList<>();
        while(true){
            synchronized (MyRunnable.class){
                if (list1.size() == 0){
                    break;
                }
                int index = r.nextInt(list1.size());
                Integer remove = list1.remove(index);
                box.add(remove);
            }
        }
        System.out.println("在此次抽奖过程中，" + thread.getName() + "总共产生了" +
                box.size() + "个奖项，分别为：" + box + "最高奖项为" + getMax(box) + "元，总计额为"+getSum(box)+"元");
        if(getMax(box) == max){
            System.out.println("在此次抽奖过程中," + thread.getName() + "中产生了最大奖项,该奖项金额为"+max+"元");
        }
    }

    private int getSum(ArrayList<Integer> box) {
         int sum = 0;
        for (Integer integer : box) {
            sum += integer;
        }
        return sum;
    }

    private int getMax(ArrayList<Integer> box) {
         int max = box.get(0);
        for (Integer integer : box) {
            if (max < integer){
                max = integer;
            }
        }
        return max;
    }
}

